<?php

/**
 * 自动生成 MerDictionaryService
 * 
 * Apps/Common/Service/MerDictionaryService.class.php
 * Zix @ 2015-06-15
 */
namespace Common\Service;

class MerDictionaryService {
  private static $instance;
  var $is_del = array (
      0 => '否',
      1 => '是' 
  );
  const USER_ROLE = 1;
  const USER_STATUS = 2;
  const CONTENT_CATALOG = 3;
  const GOODS_CATALOG = 1;
  public static function instance() {
    if (self::$instance == null) {
      $instance = new MerDictionaryService ();
    }
    
    return $instance;
  }
  function get_default_row() {
    return array (
        'pid' => '',
        'sort' => '99',
        'text' => '',
        'mer_id' => '',
        'tip' => '',
        'create_date' => date ( 'Y-m-d H:i:s' ),
        'update_date' => date ( 'Y-m-d H:i:s' ),
        'expand' => '' 
    );
  }
  function get_by_id($id) {
    $MerDictionary = M ( 'MerDictionary' );
    $data = $MerDictionary->find ( $id );
    return $data ? $data : array ();
  }
  function get_by_cond($config) {
    $default = array (
        'page' => 1,
        'page_size' => 6,
        'mer_id' => 0,
        'status' => '',
        'pid' => 0,
        'com_id' => 0,
        'count' => FALSE,
        'order' => 'ASC',
        'sort' => 'sort' 
    );
    
    $config = extend ( $config, $default );
    
    $MerDictionary = M ( 'MerDictionary' );
    
    $where = array ();
    
    if (empty ( $config ['pid'] )) {
      $where ['pid'] = array (
          'exp',
          'is null' 
      );
    } else {
      $where ['pid'] = $config ['pid'];
    }
    
    if (! empty ( $config ['pid'] ) && ! empty ( $config ['mer_id'] )) {
      $mer_id = $config ['mer_id'];
      $where ['_string'] = " mer_id = $mer_id or mer_id is null ";
    }
    
    if (! empty ( $config ['status'] )) {
      $where ['status'] = $config ['status'];
    }
    
    if ($config ['count']) {
      return $MerDictionary->where ( $where )
        ->count ();
    } else {
      $order = $config ['sort'] . ' ' . $config ['order'];
      $limit = ($config ['page'] - 1) * $config ['page_size'] . ' , ' . $config ['page_size'];
      $data = $MerDictionary->where ( $where )
        ->limit ( $limit )
        ->order ( $order )
        ->select ();
      // echo $ComDictionary->_sql();
      return $data ? $data : array ();
    }
  }
  function create($data) {
    $MerDictionary = M ( 'MerDictionary' );
    $ret = $MerDictionary->add ( $data );
    if ($ret) {
      return ajax_arr ( '添加成功', TRUE, array (
          'id' => $ret 
      ) );
    } else {
      return ajax_arr ( '添加失败', FALSE );
    }
  }
  function update($id, $data) {
    $MerDictionary = M ( 'MerDictionary' );
    
    $ret = $MerDictionary->where ( "id = %d", $id )
      ->save ( $data );
    if ($ret) {
      return ajax_arr ( '编辑成功', TRUE );
    } else {
      return ajax_arr ( '编辑失败', FALSE );
    }
  }
  function delete($ids) {
    $MerDictionary = M ( 'MerDictionary' );
    $ret = $MerDictionary->delete ( $ids );
    if ($ret == 0) {
      return ajax_arr ( '未删除任何数据', FALSE );
    } else if (! $ret) {
      return ajax_arr ( '删除失败', FALSE );
    } else {
      return ajax_arr ( '删除' . $ret . '行数据', TRUE );
    }
  }
  function _to_key_val($data) {
    $arr = array ();
    foreach ( $data as $row ) {
      $arr [$row ['id']] = $row ['text'];
    }
    
    return $arr;
  }
  function _get_by_merchant_pid($mer_id, $pid, $is_key_value = TRUE) {
    $MerDictionary = M ( 'MerDictionary' );
    $data = $MerDictionary->where ( "(mer_id = %d or mer_id is null )and pid =%d ", $mer_id, $pid )
      ->order ( 'sort ASC' )
      ->select ();
    // echo $MerDictionary->_sql();
    if ($is_key_value) {
      return $this->_to_key_val ( $data );
    }
    return $data;
  }
  
  /**
   * 获取字典枚举
   *
   * @param type $mer_id
   *          企业编号
   * @param type $text
   *          枚举名称
   * @return 枚举集合
   */
  function get_dictionaries_by_text($mer_id, $text) {
    $MerDictionary = M ( 'MerDictionary' );
    $data = $MerDictionary->field ( "d1.*" )
      ->alias ( 'd1' )
      ->join ( "mer_Dictionary d2 on d1.pid = d2.id", "left" )
      ->where ( "d1.mer_id = %d and d2.text = '%s' ", $mer_id, $text )
      ->order ( 'sort ASC' )
      ->select ();
    // echo $MerDictionary->_sql();
    if ($is_key_value) {
      return $this->_to_key_val ( $data );
    }
    return $data;
  }
  
  /**
   * 取用户角色
   */
  function get_user_role($mer_id, $is_key_value = TRUE) {
    return $this->_get_by_merchant_pid ( $mer_id, self::USER_ROLE, $is_key_value );
  }
  
  /**
   * 取用户状态
   */
  function get_user_status($mer_id, $is_key_value = TRUE) {
    return $this->_get_by_merchant_pid ( $mer_id, self::USER_STATUS, $is_key_value );
  }
  
  /**
   * 取内容分类
   */
  function get_content_catalog($mer_id, $is_key_value = TRUE) {
    return $this->_get_by_merchant_pid ( $mer_id, self::CONTENT_CATALOG, $is_key_value );
  }
  
  /**
   * 取内容分类
   */
  function get_goods_catalog($mer_id, $is_key_value = TRUE) {
    return $this->_get_by_merchant_pid ( $mer_id, self::GOODS_CATALOG, $is_key_value );
  }
}
